package com.laozhao.framework.snow;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class SnowUtil {
	private static Map<String, PageModel> pageMap = new HashMap<String, PageModel>();
	private static String xmlPath = "";

	private static PageModel getPageModel(String id) {
		if (pageMap.keySet().contains(id)) {
			return pageMap.get(id);
		}
		return null;
	}

	public static void loadConfig(String... xmlPaths) {
		try {
			List<Element> elements = new ArrayList<Element>();
			for (String xmlPath : xmlPaths) {
				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse(new File((xmlPath)));
				// Document doc = db.parse(xmlFile);
				elements.add(doc.getDocumentElement());
			}

			loadSystem(elements);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void loadSystem(List<Element> elements) {
		if (elements != null) {
			for (Element element : elements) {
				// load pages
				NodeList pagesNode = element.getChildNodes();
				PageModel pageModel = null;
				String pageId;
				String pageName;
				String pageIcon;
				String pageDescription;
				String pageType;
				Map<String, AreaModel> pageAreas;
				for (int i = 0; i < pagesNode.getLength(); i++) {
					// load page
					org.w3c.dom.Node pageNode = pagesNode.item(i);
					if (pageNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
							&& pageNode.getNodeName().equalsIgnoreCase("page")) {
						// page id
						pageId = getStringAttribute(pageNode,
								PageModel.ATTRIBUTE_ID);
						// page name
						pageName = getStringAttribute(pageNode,
								PageModel.ATTRIBUTE_NAME);
						// page icon
						pageIcon = getStringAttribute(pageNode,
								PageModel.ATTRIBUTE_ICON);
						// page description
						pageDescription = getStringAttribute(pageNode,
								PageModel.ATTRIBUTE_DESCRIPTION);
						// page type
						pageType = getStringAttribute(pageNode,
								PageModel.ATTRIBUTE_TYPE);
						// load page areas
						pageAreas = new HashMap<String, AreaModel>();
						AreaModel areaModel = null;
						String areaId;
						String areaName;
						String areaIcon;
						String areaDescription;
						String areaType;
						String areaParam;
						String areaValue;
						Integer areaRow;
						Integer areaColumn;

						List<ItemModel> areaItems;
						// ItemModel itemModel;
						org.w3c.dom.NodeList areasNode = pageNode
								.getChildNodes();
						for (int j = 0; j < areasNode.getLength(); j++) {
							org.w3c.dom.Node areaNode = areasNode.item(j);
							if (areaNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
									&& areaNode.getNodeName().equalsIgnoreCase(
											"area")) {
								// area id
								areaId = getStringAttribute(areaNode,
										AreaModel.ATTRIBUTE_ID);
								// area name
								areaName = getStringAttribute(areaNode,
										AreaModel.ATTRIBUTE_NAME);
								// area icon
								areaIcon = getStringAttribute(areaNode,
										AreaModel.ATTRIBUTE_ICON);
								// area description
								areaDescription = getStringAttribute(areaNode,
										AreaModel.ATTRIBUTE_DESCRIPTION);
								// area type
								areaType = getStringAttribute(areaNode,
										AreaModel.ATTRIBUTE_DESCRIPTION);
								// area param
								areaParam = getStringAttribute(areaNode,
										AreaModel.ATTRIBUTE_PARAM);
								// area value
								areaValue = getStringAttribute(areaNode,
										AreaModel.ATTRIBUTE_VALUE);
								// area row
								areaRow = getIntAttribute(areaNode,
										AreaModel.ATTRIBUTE_ROW);
								// area column
								areaColumn = getIntAttribute(areaNode,
										AreaModel.ATTRIBUTE_COLUMN);
								// area page items
								areaItems = new ArrayList<ItemModel>();
								ItemModel itemModel = null;
								String itemId;
								String itemName;
								String itemIcon;
								String itemDescription;
								String itemType;
								String itemParam;
								String itemValue;
								boolean itemEdit;
								String itemAction;
								String itemCommand;
								org.w3c.dom.NodeList itemsNode = areaNode
										.getChildNodes();
								for (int k = 0; k < itemsNode.getLength(); k++) {
									org.w3c.dom.Node itemNode = areasNode
											.item(k);
									if (itemNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
											&& itemNode.getNodeName()
													.equalsIgnoreCase("item")) {
										// item id
										itemId = getStringAttribute(itemNode,
												ItemModel.ATTRIBUTE_ID);
										// item name
										itemName = getStringAttribute(itemNode,
												ItemModel.ATTRIBUTE_NAME);
										// item icon
										itemIcon = getStringAttribute(itemNode,
												ItemModel.ATTRIBUTE_ICON);
										// item description
										itemDescription = getStringAttribute(
												itemNode,
												ItemModel.ATTRIBUTE_DESCRIPTION);
										// item type
										itemType = getStringAttribute(itemNode,
												ItemModel.ATTRIBUTE_TYPE);
										// item param
										itemParam = getStringAttribute(
												itemNode,
												ItemModel.ATTRIBUTE_PARAM);
										// item value
										itemValue = getStringAttribute(
												itemNode,
												ItemModel.ATTRIBUTE_VALUE);
										// item edit
										itemEdit = getBooleanAttribute(
												itemNode,
												ItemModel.ATTRIBUTE_EDIT);
										// item action
										itemAction = getStringAttribute(
												itemNode,
												ItemModel.ATTRIBUTE_ACTION);
										// item command
										itemCommand = getStringAttribute(
												itemNode,
												ItemModel.ATTRIBUTE_COMMAND);
										itemModel = new ItemModel(itemId,
												itemName, itemIcon,
												itemDescription, itemType,
												itemParam, itemValue, itemEdit,
												itemAction, itemCommand);
										
										areaItems.add(itemModel);
									}
								}
								areaModel = new AreaModel(areaId, areaName,
										areaIcon, areaDescription, areaType,
										areaParam, areaValue, areaRow,
										areaColumn, areaItems);
								pageAreas.put(areaModel.getId(), areaModel);
							}
						}
						pageModel = new PageModel(pageId, pageName, pageIcon,
								pageDescription, pageType, pageAreas);
					}

					pageMap.put(pageModel.getId(), pageModel);
				}
			}
		}
	}

	public static String getStringAttribute(org.w3c.dom.Node node, String name) {
		org.w3c.dom.Node attribute = node.getAttributes().getNamedItem(name);
		if (attribute != null) {
			return attribute.getNodeValue();
		} else {
			return null;
		}
	}

	public static int getIntAttribute(org.w3c.dom.Node node, String name) {
		String value = getStringAttribute(node, name);
		if (value != null && !value.isEmpty()) {
			return Integer.valueOf(value);
		}
		return 0;
	}

	public static boolean getBooleanAttribute(org.w3c.dom.Node node, String name) {
		String value = getStringAttribute(node, name);
		if (value != null && !value.isEmpty()) {
			return Boolean.valueOf(value);
		}
		return false;
	}
}
